<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>宝贝投-P2P平台->运营后台</title>
<!-- links begin -->
<link rel="shortcut icon" href="../favicon.ico" />
<link rel="stylesheet" href="../js/bootstrap-3.3.2-dist/css/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="../css/core.css" type="text/css" />
<link type="text/css" rel="stylesheet" href="../css/account.css" />

<script type="text/javascript" src="../js/jquery/jquery-2.1.3.js"></script>
<script type="text/javascript" src="../js/bootstrap-3.3.2-dist/js/bootstrap.js"></script>
<script type="text/javascript" src="../js/jquery.bootstrap.js"></script>
<!-- links end -->
<script type="text/javascript" src="../js/plugins/jquery.form.js"></script>
<script type="text/javascript" src="../js/plugins/jquery.twbsPagination.min.js"></script>
<script type="text/javascript" src="../js/My97DatePicker/WdatePicker.js" ></script>
<script type="text/javascript" src="../js/common.js"></script>

</head>
<body>
	<div class="container">
		<!-- 网页顶部 begin -->
		<div class="mgr-header"></div>
		<!-- 网页顶部 end -->
		<div class="row">
			<!--左侧菜单栏 begin -->
			<div class="mgr-leftmenu col-sm-3"></div>
			<!--左侧菜单栏 end -->
			<div class="col-sm-9">
				<div class="page-header">
					<h3>借款审核</h3>
				</div>
				<!-- 查询面板 Begin -->
				<form action="http://localhost:8080/finance/borrow/query" name="searchForm" id="searchForm" class="form-inline">
					<div class="form-group">
						<label>申请日期:</label>
						<input type="text" id="beginDate" name="beginDate" class="form-control" style="cursor: pointer; background:#fff;" readonly="true" autocomplete="off" value='' />
					</div>
					<div class="form-group">
						<label>&emsp;至&emsp;</label>
						<input type="text" id="endDate" name="endDate" class="form-control" style="cursor: pointer; background:#fff;" readonly="true" autocomplete="off" value='' />
					</div>
					<div class="form-group">
						<label>&emsp;状态:</label>
						<select class="form-control" id="borrowStates" name="borrowStates">
							<option value="">全部</option>
							<option value="10">发布审核中</option>
							<option value="11">发布审核拒绝</option>
							<option value="20">招标中</option>
							<option value="21">流标</option>
							<option value="30">放款审核中</option>
							<option value="31">放款审核拒绝</option>
							<option value="40">还款中</option>
							<option value="50">已完成</option>
						</select>
					</div>
					<div class="form-group">
						&emsp;&emsp;
						<button type="button" id="query" class="btn btn-success">
							<i class="icon-search"></i>
							查询
						</button>
					</div>
				</form>
				<!-- 查询面板 End -->
				<div class="panel panel-default" style="margin-top: 20px;">
					<table id="tblBorrow" class="table table-striped">
						<thead>
							<tr>
								<th>标题</th>
								<th width="60px">借款人</th>
								<th width="80px">借款金额</th>
								<th width="50px">期数</th>
								<th width="60px">年化率</th>
								<th width="80px">总利息</th>
								<th>申请时间</th>
								<th width="100px">操作</th>
							</tr>
						</thead>
						<tbody></tbody>
					</table>
					<div id="divPagination" style="text-align: center;">
						<ul id="pagination" class="pagination"></ul>
					</div>
				</div>
			</div>
		</div>

		<!-- '借款审核' 模态窗口 -->
		<div class="modal fade" id="borrowAuditModal" tabindex="-1" role="dialog">
		  <div class="modal-dialog modal-lg" role="document">
		    <div class="modal-content">
		      <div class="modal-body">
		      	<form class="form-horizontal" id="editform" method="post" action="http://localhost:8080//finance/borrow/audit">
					<fieldset>
						<div id="legend" class="">
							<legend>借款审核</legend>
						</div>
						<input type="hidden" id="borrowId" name="borrowId" value="" />
						<input type="hidden" id="borrowState" name="borrowState" value="" />
						<div class="form-group">
				        	<label class="col-sm-2 control-label">标题</label>
				        	<div class="col-sm-6">
								<p id="title" class="form-control-static">加载中...</p>
				        	</div>
			        	</div>
			        	<div class="form-group">
				        	<label class="col-sm-2 control-label">借款人</label>
				        	<div class="col-sm-6">
								<p id="borrowUsername" class="form-control-static">加载中...</p>
				        	</div>
			        	</div>
			        	<div class="form-group">
				        	<label class="col-sm-2 control-label">借款金额</label>
				        	<div class="col-sm-6">
								<p id="borrowAmount" class="form-control-static">加载中...</p>
				        	</div>
			        	</div>
			        	<div class="form-group">
				        	<label class="col-sm-2 control-label">期数</label>
				        	<div class="col-sm-6">
								<p id="repaymentMonth" class="form-control-static">加载中...</p>
				        	</div>
			        	</div>
			        	<div class="form-group">
				        	<label class="col-sm-2 control-label">还款方式</label>
				        	<div class="col-sm-6">
								<p id="repaymentType" class="form-control-static">加载中...</p>
				        	</div>
			        	</div>
			        	<div class="form-group">
				        	<label class="col-sm-2 control-label">利率</label>
				        	<div class="col-sm-6">
								<p id="yearRate" class="form-control-static">加载中...</p>
				        	</div>
			        	</div>
			        	<div class="form-group">
				        	<label class="col-sm-2 control-label">总利息</label>
				        	<div class="col-sm-6">
								<p id="totalInterest" class="form-control-static">加载中...</p>
				        	</div>
			        	</div>
					</fieldset>
				</form>
		      </div>
		      <div class="modal-footer">
		        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
		        <button id="btnAuditPass" type="button" class="btn btn-success">审核通过</button>
		        <button id="btnAuditReject" type="button" class="btn btn-warning">审核拒绝</button>
		      </div>
		    </div>
		  </div>
		</div>
	</div>
</body>
<script type="text/javascript">

// '借款信息'列表
let borrowList = new Array();

// 查询条件对象
let queryObject = new Object();

// 判断用户是否已经登录
let jsonUser = sessionStorage.getItem("admin");
if(!jsonUser) window.location.href = 'login.html';

// 激活当前页面左侧菜单栏选中效果
sessionStorage.setItem('leftMenuName', 'borrow-audit');

$(function() {

    // 加载公共顶部模板
    $('.mgr-header').load('tpl-head.html');

    // 加载左侧菜单栏模板
    $('.mgr-leftmenu').load('tpl-leftmenu.html');

    // 时间日期组件
    $("#beginDate").click(function(){
        WdatePicker();
    });

    $("#endDate").click(function(){
        WdatePicker();
    });

    // 初始化查询时间

    // 开始时间(最近30天)
    let beginDate = new Date();
    beginDate.setDate(beginDate.getDate() - 30);

    // 结束时间(今天)
    let endDate = new Date();

    $("#beginDate").val(beginDate.Format('yyyy-MM-dd'));
    $("#endDate").val(endDate.Format('yyyy-MM-dd'));


    // 注册查询按钮事件
    $("#query").click(function(){

        // 获取查询数据
        let beginDate = $("#beginDate").val();
        let endDate = $("#endDate").val();
        let borrowStates = $("#borrowStates").val();

        // 封装查询请求数据
        queryObject.beginDate = beginDate + ' 00:00:00';
        queryObject.endDate = endDate + ' 23:59:59';
        queryObject.borrowStates = borrowStates;
        queryObject.currentPage  = 1;

        // 加载'借款记录'数据
        loadBorrow();

    });// $("#query").click(function());

    // '审核通过'按钮注册事件
    $("#btnAuditPass").click(function(){

        // 设置'审核状态' value值
        $('#borrowState').val(BORROW_STATE_CONST.PUBLISH_AUDIT_PASS);

        // 隐藏'充值审核' 模态窗
        $('#borrowAuditModal').modal('hide');

        // 进行'充值审核' 表单的ajax提交
        $("#editform").ajaxSubmit(function(result){

            // 如果返回的数据的响应码不是'成功(200)'
            if(result.code != 200) {
                $.messager.alert(result.msg);
                return;
            }

            // 如果返回的响应码为'成功(200)'
            $.messager.alert('操作成功！');

            // 加载'借款审核'数据
            loadBorrow();

        });// $("#editform").ajaxSubmit();

        return false;
    });// $("#btnAuditPass").click();


    // '审核拒绝'按钮注册事件
    $("#btnAuditReject").click(function(){

        // 设置'审核状态' value值
        $('#borrowState').val(BORROW_STATE_CONST.PUBLISH_AUDIT_REJECT);

        // 隐藏'充值审核' 模态窗
        $('#borrowAuditModal').modal('hide');

        // 进行'充值审核' 表单的ajax提交
        $("#editform").ajaxSubmit(function(result){

            // 如果返回的数据的响应码不是'成功(200)'
            if(result.code != 200) {
                $.messager.alert(result.msg);
                return;
            }

            // 如果返回的响应码为'成功(200)'
            $.messager.alert('操作成功！');

            // 加载'借款审核'数据
            loadBorrow();

        });// $("#editform").ajaxSubmit();

        return false;
    });// $("#btnAuditReject").click();

    // 加载'借款记录'数据
    loadBorrow();

});// $(function());

/** 加载'借款记录'数据 **/
function loadBorrow(){

    // 加载'借款记录'数据
    $.post('http://localhost:8080/finance/borrow/query', queryObject, function (result) {

        // 如果返回的数据的响应码不是'成功(200)'
        if(result.code != 200) {
            $.messager.alert(result.msg);
            return;
        }

        // 如果返回的数据响应码为200
        // 就进行表格数据填充
        let pageResult = result.data;

        // 将分页集合赋值给全局变量
        borrowList = pageResult;

        // 填充表格数据
        fillBorrowTable( pageResult );

        // 初始化分页插件
        $("#pagination").remove();
        $("#divPagination").append('<ul id="pagination" class="pagination"></ul>');

        $("#pagination").twbsPagination({
            totalPages: result.pages.totalPageCount,
            visiblePages: 10,
            startPage: result.pages.currentPageNo,
            first: '首页',
            last: '最后一页',
            prev: '上一页',
            next: '下一页',
            onPageClick:function( event, page ){
                // 加载指定页码数据
                queryObject.currentPage = page;
                loadBorrow();

            }// onPageClick();

        });// $("#pagination").twbsPagination();

    });// $.post();

}// loadBorrow();

/** 填充表格数据 **/
function fillBorrowTable( listData ) {

    // 清空表格数据
    $('#tblBorrow tbody').empty();

    // 如果没有集合数据，就直接返回
    if( !listData || listData.length == 0 ){
        // 设置没有数据提示
        $('#tblBorrow tbody').html('<tr><td colspan="8" align="center"><p class="text-danger">暂时没有借款申请</p></td></tr>');
        return;
    }// if();

    // 循环构造单元格数据，并进行填充
    for( let i = 0; i < listData.length; i++ ) {

        let borrow = listData[i];

        let tdTitle = '<td><a target="_blank" href="borrow-info.html?id=' + borrow.id + '">' + borrow.title + '</a>&nbsp;<span class="label label-primary">信</span></td>';
        let tdBorrowUsername = '<td>' + borrow.borrowUsername + '</td>';
        let tdApplyTime = '<td>' + borrow.applyTime + '</td>';

        // 借款金额
        let borrowAmount = borrow.borrowAmount / MONEY_UNIT;
        let tdBorrowAmount = '<td>' + borrowAmount + '元</td>';

        // 借款期数
		let tdRepaymentMonth = '<td>' + borrow.repaymentMonth + '期</td>';

		// 年化利率
		let tdYearRate = '<td>' + borrow.yearRate + '%</td>';

		// 总利息
		let totalInterest = borrow.totalInterest / MONEY_UNIT;
		let tdTotalInterest = '<td>' + totalInterest + '元</td>';

        // '审核'按钮
        let tdEdit = '<td>' + BORROW_STATE[borrow.borrowState] + '</td>';

        // 如果审核状态是'审核中'，就构建1个可以点击的'审核'按钮
        if( borrow.borrowState == BORROW_STATE_CONST.PUBLISH_PENDING )
            tdEdit = '<td><a href="javascript:showBorrowAuditModal(\'' + borrow.id + '\');">审核</td></td>';

        // 将构造好的单元格数据，填充到表格中
        $('#tblBorrow tbody').append('<tr>' + tdTitle + tdBorrowUsername + tdBorrowAmount + tdRepaymentMonth + tdYearRate + tdTotalInterest + tdApplyTime + tdEdit + '</tr>');

    }// for();

}// fillBorrowTable( listData );

/** 弹出'借款审核' 模态窗 **/
function showBorrowAuditModal( id ){

    let borrow;

    // 遍历'借款信息'集合，找到匹配的id
    for( let i = 0; i < borrowList.length; i++ ){

        let tmpObj = borrowList[i];

        if( tmpObj.id == id ){
            borrow = tmpObj;
            break;
        }

    }// for();

    // 将找到的匹配数据填充到'编辑'表单上
    $('#borrowId').val(borrow.id);
    $('#title').text(borrow.title);
    $('#borrowUsername').text(borrow.borrowUsername);
    $('#borrowAmount').text(borrow.borrowAmount / MONEY_UNIT + '元');
    $('#repaymentMonth').text(borrow.repaymentMonth + '期');
    $('#repaymentType').text(REPAYMENT_TYPE[borrow.repaymentType]);
    $('#yearRate').text(borrow.yearRate + '%');
    $('#totalInterest').text(borrow.totalInterest / MONEY_UNIT + '元');


    // 显示'借款审核'编辑窗口
    $("#borrowAuditModal").modal("show");

    return false;

}// showBorrowAuditModal();
</script>
</html>